package com.bdyc.service.springImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.bdyc.controller.custom.ManagerInfoPo;
import com.bdyc.dao.ManagerInfoMapper;
import com.bdyc.dao.ManagerRoleRelMapper;
import com.bdyc.po.ManagerInfo;
import com.bdyc.po.ManagerRoleRel;
import com.bdyc.service.spring.ManagerInfoService;
@Service("managerInfoService")
public class ManagerInfoServiceImpl implements ManagerInfoService {

	@Autowired(required=true)
	@Qualifier("managerInfoMapper")
	private ManagerInfoMapper managerInfoMapper;
	@Autowired(required=true)
	@Qualifier("managerRoleRelMapper")
	private ManagerRoleRelMapper managerRoleRelMapper;
	public void addManagerInfo(ManagerInfo managerInfo,Integer roleId) {	
		//插入manager表
		managerInfoMapper.insert(managerInfo);
		//插入manager_role_rel表
		ManagerRoleRel managerRoleRel = new ManagerRoleRel();
		managerRoleRel.setMid(managerInfo.getId());
		managerRoleRel.setRid(roleId);
		managerRoleRelMapper.insert(managerRoleRel);
	}
	public Integer selectName(String name) {
		return managerInfoMapper.selectName(name);
	}
	public ManagerInfoPo login(String name, String pass) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", name);
		map.put("pass",pass);
		return managerInfoMapper.login(map);
	}
	/**查询managerInfoList方法*/
	public List<ManagerInfoPo> findUserInfoList(Map<String, Object> sendMap) {
		//设置limit起始索引 (currentPage-1)*pageSize
		Integer currentPage = (Integer)sendMap.get("page");
		Integer pageSize  = (Integer)sendMap.get("rows");
		//封装查询记录数map
		Integer count = managerInfoMapper.findManagerInfoListCount(sendMap);
		if(count >0){
			Integer index = count/pageSize;
			System.out.println(count%pageSize);
			Integer sum = count%pageSize==0?index:index +1;
			if(currentPage == null || currentPage <= 0){
				currentPage =1;
			}
			if(currentPage > sum ){
				currentPage=sum ;
			}
			sendMap.put("currentPage",  (currentPage-1)*pageSize);
			sendMap.put("pageSize", pageSize);
		}
		return managerInfoMapper.findManagerInfoList(sendMap);
	}
	/**查询managerInfoList总记录数方法*/
	public Integer findUserInfoListCount(Map<String, Object> sendMap) {
		return managerInfoMapper.findManagerInfoListCount(sendMap);
	}

}

